﻿using System.Collections;
using IceDog.Lib.NestedClass.GeometricShape.Shape;

namespace IceDog.Lib.NestedClass.GeometricShape
{
    /// <summary>
    /// 图形列表
    /// </summary>
    public class ShapeList : IEnumerable
    {
        /// <summary>
        /// 迭代器
        /// </summary>
        private class Iterator : IEnumerator
        {
            private readonly ShapeList _drawing;
            private int _current;

            internal Iterator(ShapeList drawing)
            {
                _drawing = drawing;
                _current = -1;
            }
            #region Implementation of IEnumerator
            /// <summary>
            /// 获取集合里面的当前元素
            /// </summary>
            /// <returns>
            /// 返回集合里面的当前元素
            /// </returns>
            public object Current
            {
                get
                {
                    return _drawing._shapes[_current];
                }
            }
            /// <summary>
            /// 枚举器移动到集合中的下一个元素
            /// </summary>
            /// <returns>
            /// true表示成功移动到集合中下一个元素，false表示移动到集合末尾
            /// </returns>
            /// <exception cref="T:System.InvalidOperationException">枚举器创建后进行修改集合操作引发</exception>
            public bool MoveNext()
            {
                if (++_current >= _drawing._shapes.Count)
                {
                    return false;
                }
                return true;
            }

            /// <summary>
            /// 把枚举器重置到初始位置，该位置在集合中第一个元素之前
            /// </summary>
            /// <exception cref="T:System.InvalidOperationException">枚举器创建后进行修改集合操作引发 </exception>
            public void Reset()
            {
                _current = -1;
            }
            #endregion
        }

        private readonly ArrayList _shapes;

        public ShapeList()
        {
            _shapes = new ArrayList();
        }
        /// <summary>
        /// 添加形状
        /// </summary>
        /// <param name="shape">形状</param>
        public void Add(AbstractShape shape)
        {
            _shapes.Add(shape);
        }
        #region Implementation of IEnumerable
        /// <summary>
        ///返回当前集合的枚举器
        /// </summary>
        /// <returns>
        /// 一个 <see cref="T:System.Collections.IEnumerator"/> 能枚举集合的枚举器
        /// </returns>
        public IEnumerator GetEnumerator()
        {
            return new Iterator(this);
        }

        #endregion

    }
}
